package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.model.ArticleLikesRecord;
import com.javatiaocao.myblog.service.LikeService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class LikeController {
    @Autowired
    LikeService likeService;

    /**
     * 点赞管理
     * @param articleLikesRecord
     * @param userDetails
     * @param request
     * @return
     */
    @GetMapping("/addArticleLike")
    public String addArticleLike(ArticleLikesRecord articleLikesRecord,
                                 @AuthenticationPrincipal UserDetails userDetails, HttpServletRequest request){
        try {

            DataMap dataMap = likeService.addArticleLike(articleLikesRecord,userDetails);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("addArticleLike is error" + e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }


}
